#!/bin/bash

# PC微信聊天应用部署脚本
# 使用方法: ./deploy.sh [dev|prod]

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# 检查Node.js版本
check_node_version() {
    print_step "检查Node.js版本..."
    
    if ! command -v node &> /dev/null; then
        print_error "Node.js未安装，请先安装Node.js 18.0.0或更高版本"
        exit 1
    fi
    
    NODE_VERSION=$(node -v | cut -d'v' -f2)
    REQUIRED_VERSION="18.0.0"
    
    if [ "$(printf '%s\n' "$REQUIRED_VERSION" "$NODE_VERSION" | sort -V | head -n1)" != "$REQUIRED_VERSION" ]; then
        print_error "Node.js版本过低，当前版本: $NODE_VERSION，要求版本: $REQUIRED_VERSION+"
        exit 1
    fi
    
    print_message "Node.js版本检查通过: $NODE_VERSION"
}

# 检查npm版本
check_npm_version() {
    print_step "检查npm版本..."
    
    if ! command -v npm &> /dev/null; then
        print_error "npm未安装"
        exit 1
    fi
    
    NPM_VERSION=$(npm -v)
    print_message "npm版本: $NPM_VERSION"
}

# 安装后端依赖
install_backend_deps() {
    print_step "安装后端依赖..."
    
    cd chat-app-backend
    
    if [ ! -f "package.json" ]; then
        print_error "后端package.json文件不存在"
        exit 1
    fi
    
    print_message "清理npm缓存..."
    npm cache clean --force
    
    if [ -d "node_modules" ]; then
        print_message "删除旧的node_modules..."
        rm -rf node_modules
    fi
    
    if [ -f "package-lock.json" ]; then
        print_message "删除旧的package-lock.json..."
        rm -f package-lock.json
    fi
    
    print_message "安装后端依赖包..."
    npm install
    
    print_message "后端依赖安装完成"
    cd ..
}

# 安装前端依赖
install_frontend_deps() {
    print_step "安装前端依赖..."
    
    cd wx-project
    
    if [ ! -f "package.json" ]; then
        print_error "前端package.json文件不存在"
        exit 1
    fi
    
    print_message "清理npm缓存..."
    npm cache clean --force
    
    if [ -d "node_modules" ]; then
        print_message "删除旧的node_modules..."
        rm -rf node_modules
    fi
    
    if [ -f "package-lock.json" ]; then
        print_message "删除旧的package-lock.json..."
        rm -f package-lock.json
    fi
    
    print_message "安装前端依赖包..."
    npm install
    
    print_message "前端依赖安装完成"
    cd ..
}

# 构建前端生产版本
build_frontend() {
    print_step "构建前端生产版本..."
    
    cd wx-project
    
    print_message "开始构建..."
    npm run build
    
    if [ -d "dist" ]; then
        print_message "前端构建完成，输出目录: wx-project/dist"
    else
        print_error "前端构建失败"
        exit 1
    fi
    
    cd ..
}

# 启动开发环境
start_dev() {
    print_step "启动开发环境..."
    
    # 检查端口是否被占用
    if lsof -Pi :5000 -sTCP:LISTEN -t >/dev/null ; then
        print_warning "端口5000已被占用，请先关闭占用该端口的进程"
        print_message "可以使用以下命令查看占用进程: lsof -i :5000"
        print_message "可以使用以下命令杀死进程: kill -9 \$(lsof -t -i:5000)"
    fi
    
    if lsof -Pi :5173 -sTCP:LISTEN -t >/dev/null ; then
        print_warning "端口5173已被占用，请先关闭占用该端口的进程"
        print_message "可以使用以下命令查看占用进程: lsof -i :5173"
        print_message "可以使用以下命令杀死进程: kill -9 \$(lsof -t -i:5173)"
    fi
    
    print_message "启动后端服务..."
    cd chat-app-backend
    npm start &
    BACKEND_PID=$!
    cd ..
    
    sleep 3
    
    print_message "启动前端开发服务器..."
    cd wx-project
    npm run dev &
    FRONTEND_PID=$!
    cd ..
    
    print_message "开发环境启动完成！"
    print_message "后端服务: http://127.0.0.1:5000"
    print_message "前端应用: http://localhost:5173"
    print_message ""
    print_message "按 Ctrl+C 停止服务"
    
    # 等待用户中断
    trap "kill $BACKEND_PID $FRONTEND_PID; exit" INT
    wait
}

# 启动生产环境
start_prod() {
    print_step "启动生产环境..."
    
    # 构建前端
    build_frontend
    
    print_message "启动后端生产服务..."
    cd chat-app-backend
    
    # 设置生产环境变量
    export NODE_ENV=production
    export PORT=5000
    
    npm start
}

# 显示帮助信息
show_help() {
    echo "PC微信聊天应用部署脚本"
    echo ""
    echo "使用方法:"
    echo "  ./deploy.sh dev     启动开发环境"
    echo "  ./deploy.sh prod    启动生产环境"
    echo "  ./deploy.sh install 仅安装依赖"
    echo "  ./deploy.sh build   仅构建前端"
    echo "  ./deploy.sh help    显示帮助信息"
    echo ""
    echo "开发环境:"
    echo "  - 后端: http://127.0.0.1:5000"
    echo "  - 前端: http://localhost:5173"
    echo "  - 支持热重载和开发工具"
    echo ""
    echo "生产环境:"
    echo "  - 构建优化的前端资源"
    echo "  - 启动生产模式后端服务"
    echo ""
    echo "测试账号 (密码统一为 123456):"
    echo "  - goddlts (教瘦)"
    echo "  - aaa123 (阿玛尼)"
    echo "  - balala123 (巴拉拉)"
    echo "  - dalao (大佬)"
    echo "  - jiajia1998 (佳佳)"
    echo "  - dabai (大白机器人)"
}

# 主函数
main() {
    echo "=========================================="
    echo "     PC微信聊天应用部署脚本 v2.0.0"
    echo "=========================================="
    echo ""
    
    # 检查参数
    if [ $# -eq 0 ]; then
        show_help
        exit 1
    fi
    
    case $1 in
        "dev")
            check_node_version
            check_npm_version
            install_backend_deps
            install_frontend_deps
            start_dev
            ;;
        "prod")
            check_node_version
            check_npm_version
            install_backend_deps
            install_frontend_deps
            start_prod
            ;;
        "install")
            check_node_version
            check_npm_version
            install_backend_deps
            install_frontend_deps
            print_message "所有依赖安装完成！"
            ;;
        "build")
            check_node_version
            check_npm_version
            build_frontend
            print_message "前端构建完成！"
            ;;
        "help")
            show_help
            ;;
        *)
            print_error "未知参数: $1"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"
